<?php
/*
 * Created on Jun 22, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class ListOfMedicalRecordItem extends ORDataObject {
 	var $_key='encounter_type_id';
	var $_table='encounter_type_department_map';
	var $_internalName='EncounterTypeDepartmentMap';
	public function ListOfMedicalRecordItem() {
		parent::ORDataObject();
	}
	/**
	 * return the list of controller and action of medical profile
	 */
	public function getListOfCtAc() {
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "encounter_type_id as id, emr_name, emr_class_name, emr_method_name",
			'from' 	=> "encounter_type"			
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
		
	}
	public function GetMedicalRecordItemById($id) {
		
	}
	public function listOfMappedDepartments() {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');		
		
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "distinct d.department_id as id, d.$field as name",
			'from' 	=> "department as d INNER JOIN encounter_type_department_map as m" .
					" ON d.department_id=m.department_id" 
			);
		$ds->setup($this->_db, $sql);		
		return $ds->toArray();
		
	}
	/**
	 * Check given department is mapped or not.
	 * @param number $depId
	 * @return boolean true: in map, false: not in map
	 */
	public function isDepInMap($depId) {
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "department_id",
			'from' 	=> "encounter_type_department_map",
			'where' => "department_id=$depId"
			);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			 return false;
		}
		return true;	
	}
	
	/**
	 * Check given emrid is mapped or not.
	 * @param number $selectedId
	 * @return boolean true: in map, false: not in map
	 */
	public function isEmrInMap($selectedId) {
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "encounter_type_id",
			'from' 	=> "encounter_type_department_map",
			'where' => "encounter_type_id=$selectedId"
			);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			 return false;
		}
		return true;	
	}
	/**
	 * List of emr.
	 * @return list of Medical record items, sorted by depatment name, emr name.
	 */
	public function listOfMedicalRecordItems() {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "distinct d.department_id as department_id, et.encounter_type_id as emr_id, 
			         et.$field as emr_name",
			'from' 	=> "department as d INNER JOIN encounter_type_department_map as m" .
					" ON d.department_id=m.department_id " .
					" INNER JOIN encounter_type as et ON et.encounter_type_id=m.encounter_type_id ",					
			'order by' => "d.$field, et.$field"
			);
		$ds->setup($this->_db, $sql);
		$resultSet = $ds->toArray();
		if (empty($resultSet)) {
			 return null;
		}
		return $resultSet;	
		
	}
	/**
	 * Get default emr of given department.
	 * @param number $depId
	 * @return int EmrId 
	 */
	public function getDefaultEmr($depId) {
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "encounter_type_id",
			'from' 	=> "encounter_type_department_map",
			'where' => "department_id=$depId && is_default > 0"
			);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			 return null;
		}
		return $result[0]['encounter_type_id'];	
		
	}
	
	public function getMappedEmrById($id) {			
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "department_id, encounter_type_id",
			'from' 	=> "encounter_type_department_map",
			'where' => "encounter_type_department_map_id=$id"
			);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			 return null;
		}
		return $result[0];				
	}
	
	public function getEmrById($emrId) {			
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "emr_class_name, emr_method_name",
			'from' 	=> "encounter_type",
			'where' => "encounter_type_id=$emrId"
			);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			 return null;
		}
		return $result[0];				
	}
	
 	
 }
?>
